package com.magoldeditor.at;

import java.awt.Color;

import com.magoldeditor.controller.Handler;
import com.magoldeditor.controller.MapHandler;
import com.magoldeditor.controller.MenuHandler;
import com.magoldeditor.controller.ShowHandler;
import com.magoldeditor.model.MaEditorMap;
import com.magoldeditor.model.entity.IEntityConstructor;
import com.magoldeditor.view.DisplayPanel;
import com.magoldeditor.view.MaEditorButton;
import com.magoldeditor.view.MaEditorFrame;
import com.magoldeditor.view.MaEditorMenuButton;
import com.magoldeditor.view.MaEditorPanel;
import com.magoldeditor.view.PaintPanel;
import com.magoldeditor.view.ShowPanel;
import com.magoldeditor.view.ViewContants;

/**
 * 
 * @author Velconia
 * @version v.2.0
 */
public class Main {
	/**
	 * components
	 */
	MaEditorFrame frame = null;
	MaEditorPanel displayPanel = null;
	MaEditorPanel showPanel = null;
	MaEditorPanel paintPanel = null;
	MaEditorButton displayButton = null;
	MaEditorButton refreshButton = null;
	MaEditorMenuButton menuButton = null;
	
	MaEditorMap map = null;
	
	/**
	 * init the components
	 */
	private void initComponents() {
		
		/*
		 * frame
		 */
		frame = new MaEditorFrame();
		
		/*
		 * panel
		 */
		displayPanel = new DisplayPanel(Color.black, "DisplayPanel");
		showPanel = new ShowPanel(Color.gray, "ShowPanel");
		
		/*
		 * button
		 */
		refreshButton = new MaEditorButton("RefreshButton");
		displayButton = new MaEditorButton("DisplayButton");
		
		/*
		 * model
		 */
		map = new MaEditorMap("Map", displayPanel, (IEntityConstructor) showPanel);
		
		/*w
		 * handler of menu button and paint panel
		 */
		MenuHandler menuHandler = new MenuHandler(map);
		
		/*
		 * menu button
		 */
		menuButton = new MaEditorMenuButton(menuHandler, "MenuButton");
		
		/*
		 * paint panel
		 */
		paintPanel = new PaintPanel(Color.green, "PaintPanel");
		
		frame.add(displayPanel);
		frame.add(showPanel);
		showPanel.add(refreshButton);
		showPanel.add(displayButton);
		showPanel.add(paintPanel);
		showPanel.add(menuButton);
		
		/*
		 * refresh the ui
		 */
		frame.repaint();
		
		ViewContants contants = ViewContants.getNewIntance();
		contants.close();
		
		System.err.println("view and model init end");
	}
	
	/**
	 * init from the config
	 */
	private void initListener() {

		Handler mapHandler = new MapHandler(displayPanel, map);
		displayPanel.setMouseListener(mapHandler);
		displayPanel.setMouseMotionListener(mapHandler);
		displayPanel.setMouseMotionWheelListener(mapHandler);
		displayPanel.setKeyListener(mapHandler);
		
		/*
		 * handler of show panel and button
		 */
		Handler showHandler = new ShowHandler(showPanel, map);
		showPanel.setMouseListener(showHandler);
		showPanel.setMouseMotionListener(showHandler);
		refreshButton.addMouseListener(showHandler);
		displayButton.addMouseListener(showHandler);
		
		System.err.println("controller init end");
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Main m = new Main();
		m.initComponents();
		m.initListener();
	}
}
